-- @Author: baidwwy
-- @Date:   2017-08-22 19:07:39
-- @Last Modified by:   baidwwy
-- @Last Modified time: 2018-08-18 03:37:34
do
local FOREGROUND_BLUE      = 0x0001 -- text color contains blue.
local FOREGROUND_GREEN     = 0x0002 -- text color contains green.
local FOREGROUND_RED       = 0x0004 -- text color contains red.
local FOREGROUND_INTENSITY = 0x0008 -- text color is intensified.
__颜色 = {
    深蓝 = FOREGROUND_BLUE;
    深绿 = FOREGROUND_GREEN;
    深青 = FOREGROUND_GREEN+FOREGROUND_BLUE;
    深红 = FOREGROUND_RED;
    深紫 = FOREGROUND_RED+FOREGROUND_BLUE;
    深黄 = FOREGROUND_RED+FOREGROUND_GREEN;
    深白 = FOREGROUND_RED+FOREGROUND_GREEN+FOREGROUND_BLUE;
    深灰 = FOREGROUND_INTENSITY;

    蓝色 = FOREGROUND_INTENSITY+FOREGROUND_BLUE;--9
    绿色 = FOREGROUND_INTENSITY+FOREGROUND_GREEN;
    青色 = FOREGROUND_INTENSITY+FOREGROUND_GREEN+FOREGROUND_BLUE;
    红色 = FOREGROUND_INTENSITY+FOREGROUND_RED;
    紫色 = FOREGROUND_INTENSITY+FOREGROUND_RED+FOREGROUND_BLUE;
    黄色 = FOREGROUND_INTENSITY+FOREGROUND_RED+FOREGROUND_GREEN;
    白色 = FOREGROUND_INTENSITY+FOREGROUND_RED+FOREGROUND_GREEN+FOREGROUND_BLUE;
}
end
local _input,_exit = {},false
function __Loop(t, ... )
    if t then
        if t == 'close' then
            if 引擎.退出函数 then
                __gge.xpcall(引擎.退出函数,引擎)
            end
        elseif 引擎.输入函数 and type(t)=='number' then
            if t==13 then
                if _input[1] and __gge.xpcall(引擎.输入函数,引擎,string.char(unpack(_input))) then
                    return true
                end
                _input = {}
            else
                table.insert(_input, t)
            end
        end
        return false
    elseif 引擎.循环函数 then
        return __gge.xpcall(引擎.循环函数,引擎) or _exit
    end
    return _exit
end
if not __gge.isdebug then
    local 已显示 = {}
    __gge.traceback = function (msg)
        if not 已显示[msg] then
            已显示[msg] = true
            if 引擎.日志 then
                引擎.写日志(debug.traceback(msg),'ERROR')
            else
                __gge.messagebox(tostring(msg),"致命的错误",16)
            end
        end
    end
end
local _hConsole = ffi.C.GetStdHandle(-11)
local _CSBI     = ffi.new('CONSOLE_SCREEN_BUFFER_INFO')
local _Written  = ffi.new('int[1]')
--================================================================
引擎 = {}

function 引擎.创建(t)
    if t.标题 then 引擎.置标题(t.标题) end
    引擎.日志 = require("logger")(t.日志)
    __gge.start(__Loop)
end
function 引擎.置颜色(color)
    ffi.C.SetConsoleTextAttribute(_hConsole,color)
end
function 引擎.取颜色()
    ffi.C.GetConsoleScreenBufferInfo(_hConsole,_CSBI)
    return _CSBI.wAttributes
end
function 引擎.置坐标(pos)
    ffi.C.SetConsoleCursorPosition(_hConsole,pos)
end
function 引擎.取坐标()
    ffi.C.GetConsoleScreenBufferInfo(_hConsole,_CSBI)
    return {_CSBI.dwCursorPosition.x,_CSBI.dwCursorPosition.y}
end
function 引擎.置标题(str)
    ffi.C.SetConsoleTitleA(str)
end
function 引擎.取标题()
    local title = ffi.new('char[512]')
    ffi.C.GetConsoleTitleA(title,512)
    return ffi.string(title)
end
function 引擎.输出(str,color,time,pos,log)
    str = tostring(str)
    if type(time) == 'boolean' then
        if time then
            str = string.format("[%s]  %s", os.date("%X", os.time()),str)
        end
    else
        pos = time
    end
    if pos then--不改变坐标
        ffi.C.WriteConsoleOutputCharacterA(_hConsole,str,#str,pos,_Written)
        if color then
            ffi.C.FillConsoleOutputAttribute(_hConsole,color,#str,pos,_Written)
        end
    elseif color then
        local 原颜色 = 引擎.取颜色()
        引擎.置颜色(color)
        ffi.C.WriteConsoleA(_hConsole,str,#str,_Written,nil)
        引擎.置颜色(原颜色)
    else
        ffi.C.WriteConsoleA(_hConsole,str,#str,_Written,nil)
    end
end
function 引擎.置缓冲区大小(Size)
    ffi.C.SetConsoleScreenBufferSize(_hConsole,Size)
    return self
end
function 引擎.置延迟()

end
function 引擎.写日志(文本,类型)
    if 引擎.日志 then
        引擎.日志:LOGS(文本,类型)
    end
end
function 引擎.文件是否存在(file)
    return ffi.C._access(file,0)==0
end

function 引擎.关闭()
    _exit = true
end
setmetatable(引擎,{__call = function (t, ... )
    return t.创建(...)
end
})

return 引擎